﻿using UnityEngine;
using UnityEngine.InputSystem.XR;

public class PlayerAirHFSM : PlayerBaseHFSM {
	Timer _timer = new();
	bool _isLeaveGround;
	public PlayerAirHFSM(PlayerController controller, PlayerStateEnum stateName, State<PlayerStateEnum> superFSM, StateFactory<PlayerStateEnum> factory) : base(controller, stateName, superFSM, factory) {
		AddConditionToSelf(() => !controller.IsOnGround);

		AddNewTransition(PlayerStateEnum.GroundHFSM, () => controller.IsOnGround && _isLeaveGround);
		AddNewTransition(PlayerStateEnum.WallHFSM, () => (controller.IsTouchingEdge || controller.IsTouchingWall) && XInputToWall);
		AddNewTransition(PlayerStateEnum.AirATKHFSM, () => AirAtkInput);
	}

	public override void Enter() {
		base.Enter();
		_timer.Reset();
		_isLeaveGround = false;
	}
	public override void Update() {
		_controller.ChangeDir();
		if (_timer.Elapsed > 0.1f) {
			_isLeaveGround = true;
		}
		_controller.Velocity = new UnityEngine.Vector2(_controller.XInput * _controller.ConfigData.AirXMoveSpeed, _controller.Velocity.y);
		base.Update();
		//_controller.ChangeDir();
		//Debug.Log($"AAAAAAAAAAAAAAAAAAAAAAAaair{_controller.Velocity.x},{_controller.XInput}");
	}

	bool XInputToWall => AreSignsEqual<float, int>(_controller.XInput, _controller.StateData.FaceDir);
	bool AirAtkInput => _controller.HandWeaponInput || _controller.TickInput || (_controller.DownInput && _controller.Jump) || _controller.BowInput;
}